//
//  MoLocalizable.swift
//  momall_Example
//
//  Created by zhu xietong on 2020/4/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation
import UIKit

public enum ThemeMode:String{
    case auto = "auto"
    case light = "light"
    case dark = "dark"
    
    
    public var indicatorStyle:UIActivityIndicatorView.Style{
        get{
            switch self {
            case .auto:
                if #available(iOS 13.0, *) {
                    return .medium
                } else {
                    return .gray
                }
            case .dark:
                return .white
            default:
                return .gray
            }
        }
    }
}

extension UIActivityIndicatorView{
    convenience init(theme:ThemeMode) {
        switch theme {
        case .auto:
            if #available(iOS 13.0, *) {
                self.init(style:.medium)
            } else {
                self.init(style:.gray)
            }
        case .dark:
            self.init(style:.white)
        default:
            self.init(style:.gray)
        }
    }
}

public class MoTheme {
    
    public var mode = ThemeMode.auto
    public func color(for color:UIColor?) -> UIColor? {
        switch mode {
        case .auto:
            return color
        case .dark:
            return color?._dark
        case .light:
            return color?._light
        }
    }
    
    public func dycolor(lColor:UIColor,dColor:UIColor) -> UIColor {
        switch mode {
        case .auto:
            return .color(lColor: lColor, dColor: dColor)
        case .dark:
            return dColor
        case .light:
            return lColor
        }
    }
    public func dycolor(light:String,dark:String) -> UIColor {
        switch mode {
        case .auto:
            return .color(light: light, dark: dark)
        case .dark:
            return UIColor(shex: dark)
        case .light:
            return UIColor(shex: light)
        }
    }
    
    public func _color(for color:UIColor) -> UIColor {
           switch mode {
           case .auto:
               return color
           case .dark:
               return color._dark
           case .light:
               return color._light
           }
       }
    
    public static let global = MoTheme()
    var _cellBackground:UIColor? = nil
    public var cellBackground:UIColor{get{return color(for: _cellBackground) ?? _color(for: UIColor._cellBackground)}set{_cellBackground = newValue}}
    
    var _separator:UIColor? = nil
    public var separator:UIColor{get{return color(for: _separator) ?? _color(for: UIColor._separator)}set{_separator = newValue}}
    
    var _tableBackground:UIColor? = nil
    public var tableBackground:UIColor{get{return color(for: _tableBackground) ?? _color(for: UIColor._tableBackground)}set{_tableBackground = newValue}}
    
    
    var _foregroundWeak:UIColor? = nil
    public var foregroundWeak:UIColor{get{return color(for: _foregroundWeak) ?? _color(for: UIColor._foregroundWeak)}set{_foregroundWeak = newValue}}
    
    var _foregroundStrong:UIColor? = nil
    public var foregroundStrong:UIColor{get{return color(for: _foregroundStrong) ?? _color(for: UIColor._foregroundStrong)}set{_foregroundStrong = newValue}}
    
    var _foreground:UIColor? = nil
    public var foreground:UIColor{get{return color(for: _foreground) ?? _color(for: UIColor._foreground)}set{_foreground = newValue}}
    
    var _backgroundWeak:UIColor? = nil
    public var backgroundWeak:UIColor{get{return color(for: _backgroundWeak) ?? _color(for: UIColor._backgroundWeak)}set{_backgroundWeak = newValue}}
    
    var _backgroundStrong:UIColor? = nil
    public var backgroundStrong:UIColor{get{return color(for: _backgroundStrong) ?? _color(for: UIColor._backgroundStrong)}set{_backgroundStrong = newValue}}
    
    var _tint:UIColor? = nil
    public var tint:UIColor{get{return color(for: _tint) ?? _color(for:  UIColor._tint)}set{_tint = newValue}}
    
    
}

extension UIView{
    
    private struct Owner_themeKey {
        static var Owner_themeKey = "Owner_themeKey"
        static var mo_controller = "Owner_themeKey_mo_controller"
    }
    public var mo_theme:MoTheme{
        get{
            if let obj = objc_getAssociatedObject(self, &Owner_themeKey.Owner_themeKey) as? MoTheme
            {
                return obj
            }
            let theme = mo_controller?.mo_theme ?? MoTheme()
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, theme, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
            return theme
        }
        set{
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
    @objc public var mo_controller:UIViewController?{
        get{
            if let obj = objc_getAssociatedObject(self, &Owner_themeKey.mo_controller) as? UIViewController
            {
                return obj
            }
            let ctr = getSelfContainerController()
            objc_setAssociatedObject(self, &Owner_themeKey.mo_controller, ctr, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
            return ctr
        }
        set{
            objc_setAssociatedObject(self, &Owner_themeKey.mo_controller, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
}

extension ASDisplayNode{
    private struct Owner_themeKey {
        static var Owner_themeKey = "Owner_themeKey"
        static var mo_controller = "Owner_themeKey_mo_controller"
    }
    
    public var mo_controller:UIViewController?{
           get{
               if let obj = objc_getAssociatedObject(self, &Owner_themeKey.mo_controller) as? UIViewController
               {
                   return obj
               }
               let ctr = getSelfContainerController()
               objc_setAssociatedObject(self, &Owner_themeKey.mo_controller, ctr, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
               return ctr
           }
           set{
               objc_setAssociatedObject(self, &Owner_themeKey.mo_controller, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
           }
    }
    public var mo_theme:MoTheme{
        get{

            if let obj = objc_getAssociatedObject(self, &Owner_themeKey.Owner_themeKey) as? MoTheme
            {
                return obj
            }
            let theme = mo_controller?.mo_theme ?? MoTheme()
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, theme, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
            return theme
        }
        set{
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
}

extension UIViewController{
    
    private struct Owner_themeKey {
        static var Owner_themeKey = "Owner_themeKey"
    }
    public var mo_theme:MoTheme{
        get{
            if let obj = objc_getAssociatedObject(self, &Owner_themeKey.Owner_themeKey) as? MoTheme
            {
                return obj
            }
            let theme = MoTheme()
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, theme, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
            return theme
        }
        set{
            objc_setAssociatedObject(self, &Owner_themeKey.Owner_themeKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
}
